package com.agger.eurekaclientuserservice.config;

import com.agger.eurekacommonapi.exception.BusinessException;
import com.agger.eurekacommonapi.exception.BusinessRuntimeException;
import com.agger.eurekacommonapi.vo.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @classname: GlobalExceptionHandler
 * @description: 全局的异常处理器
 * @author chenhx
 * @date 2020-05-26 11:01:03
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public ResultData handleCommonException(Exception ex) {
        log.error("未捕获的异常：", ex);
        return ResultData.FAILED(ex.getMessage(), ex);
    }

    @ExceptionHandler({BusinessException.class})
    @ResponseBody
    public ResultData handleBusinessException(BusinessException ex) {
        log.error("业务异常：", ex);
        return ResultData.FAILED(ex.getMessage());
    }

    @ExceptionHandler({BusinessRuntimeException.class})
    @ResponseBody
    public ResultData handleBusinessRuntimeException(BusinessRuntimeException ex) {
        log.error("运行业务异常：", ex);
        return ResultData.FAILED(ex.getMessage(),ex);
    }

}
